Return reverse string


Posted by Christy on 2022-04-21

Description: Write a function named reverse that accepts a string and return the reversed string

Note: Don’t use the built-in function reverse()

function reverse(str) {
  let result = "";
  for (let i = str.length - 1; i >= 0; i--) {
    result += str[i];
  }
  return result;
}

console.log(reverse("abcd")); // dcba
console.log(reverse("12345aa")); // aa54321









Related Posts

[Week 2] JavaScript - 函式 function

[Week 2] JavaScript - 函式 function

MTR04_1117

MTR04_1117

[30] 文法 - 自動分號、TDZ、try..finally、switch

[30] 文法 - 自動分號、TDZ、try..finally、switch


Comments